<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport"
    content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
  <meta name="author" content="火星科技 http://mars3d.cn ">
  <meta name="apple-touch-fullscreen" content="yes">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no">
  <meta name="x5-fullscreen" content="true">
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
  <!-- 标题及搜索关键字 -->
  <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
  <meta name="description"
    content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS" />

  <link rel="shortcut icon" type="image/x-icon" href="http://mars3d.cn/favicon.ico">
  <title>三维地图 | Mars3D | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>


  <!--第三方lib-->
  <script type="text/javascript" src="../lib/include-lib.js" libpath="../lib/"
    include="jquery,jquery.range,font-awesome,bootstrap,bootstrap-checkbox,layer,toastr,haoutil,turf,mars3d"></script>

  <link href="css/style.css" rel="stylesheet" />
</head>

<body class="dark">
  <!--加载前进行操作提示，优化用户体验-->
  <div id="mask" class="signmask" onclick="removeMask()"></div>

  <div id="mars3dContainer" class="mars3d-container"></div>


  <!-- 面板 -->
  <div class="infoview">
    <table class="mars-table">
      <tr>
        <td class="nametd">轨迹方向（heading）</td>
        <td>
          <input id="model_heading" type="range" min="0" max="360" step="0.01" value="220" title="方向heading">
        </td>
        <td>
          <input id="model_heading_2" type="number" min="0" max="360" step="0.01" value="220" title="方向heading">
        </td>
      </tr>
      <tr>
        <td class="nametd">前后侧摆（pitch）</td>
        <td>
          <input id="model_pitch" type="range" min="-180" max="180" step="0.01" value="75" title="前后侧摆pitch">
        </td>
        <td>
          <input id="model_pitch_2" type="number" min="-180" max="180" step="0.01" value="75" title="前后侧摆pitch">
        </td>
      </tr>
      <tr>
        <td class="nametd">左右侧摆（roll）</td>
        <td>
          <input id="model_roll" type="range" min="-180" max="180" step="0.01" value="0" title="左右侧摆roll">
        </td>
        <td>
          <input id="model_roll_2" type="number" min="-180" max="180" step="0.01" value="0" title="左右侧摆roll">
        </td>
      </tr>

      <tr class="coneEntityAttr">
        <td colspan="2">
          <button id="btnSePoint" class="btn btn-primary">设置摄像头位置</button>
          <button id="btnGetCenter" class="btn btn-primary">计算与地面交点</button>
        </td>

        <td>
          <div class="checkbox checkbox-primary checkbox-inline">
            <input id="chkTestTerrain" class="styled" type="checkbox">
            <label for="chkTestTerrain">
              深度检测
            </label>
          </div>
        </td>
      </tr>
    </table>
  </div>



  <script src="./js/common.js"></script>
  <script type="text/javascript">
    'use script' //开发环境建议开启严格模式

    var map
    var sightline

    var positionSXT
    var positionDM
    var positionJD //与地面的交点

    function initMap(options) {
      //合并属性参数，可覆盖config.json中的对应配置
      var mapOptions = mars3d.Util.merge(options, {
        scene: {
          //此处参数会覆盖config.json中的对应配置
          center: { y: 30.841574, x: 116.18792, z: 6828.32, heading: 215.3, pitch: -28.1, roll: 359.9 },
        },
      })

      //创建三维地球场景
      map = new mars3d.Map('mars3dContainer', mapOptions)

      sightline = new mars3d.thing.Sightline()
      map.addThing(sightline)

      sightline.on(mars3d.EventType.end, function (e) {
        positionJD = e.position
      })

      //测试数据
      positionSXT = Cesium.Cartesian3.fromDegrees(116.144409, 30.744228, 1045)

      var graphicSXT = new mars3d.graphic.PointEntity({
        position: positionSXT,
        style: {
          color: '#ffff00',
          pixelSize: 8,
          label: {
            text: '摄像头',
            font_size: 20,
            color: '#ffffff',
            outline: true,
            outlineColor: '#000000',
            pixelOffsetY: -20,
          },
        },
      })
      map.graphicLayer.addGraphic(graphicSXT)
      updateModel()

      //滑动条
      $('#model_heading').range({
        onChange: function (value, bfb) {
          $('#model_heading_2').val(value)
          updateModel()
        },
      })
      $('#model_heading_2').change(function () {
        var value = Number($(this).val())
        $('#model_heading').val(value)
        updateModel()
      })

      $('#model_pitch').range({
        onChange: function (value, bfb) {
          $('#model_pitch_2').val(value)
          updateModel()
        },
      })
      $('#model_pitch_2').change(function () {
        var value = Number($(this).val())
        $('#model_pitch').val(value)
        updateModel()
      })

      $('#model_roll').range({
        onChange: function (value, bfb) {
          $('#model_roll_2').val(value)
          updateModel()
        },
      })
      $('#model_roll_2').change(function () {
        var value = Number($(this).val())
        $('#model_roll').val(value)
        updateModel()
      })

      $('#chkTestTerrain').change(function () {
        var val = $(this).is(':checked')
        map.scene.globe.depthTestAgainstTerrain = val
        if (val) {
          haoutil.msg('深度监测打开后，您将无法看到地下或被地形遮挡的对象。')
        }
      })

      //创建矢量数据图层
      var graphicLayer = new mars3d.layer.GraphicLayer()
      map.addLayer(graphicLayer)

      //地面点
      var graphic = new mars3d.graphic.PointEntity({
        position: new Cesium.CallbackProperty(function (time) {
          return positionDM
        }, false),
        style: {
          color: '#0000ff',
          pixelSize: 7,
          outlineColor: '#ffffff',
          outlineWidth: 2,
          label: {
            text: '目标参考点',
            font_size: 18,
            color: '#ffffff',
            pixelOffsetY: -10,
            distanceDisplayCondition: true,
            distanceDisplayCondition_far: 500000,
            distanceDisplayCondition_near: 0,
          },
        },
      })
      graphicLayer.addGraphic(graphic)

      var graphicJD = new mars3d.graphic.PointEntity({
        position: new Cesium.CallbackProperty(function (time) {
          return positionJD
        }, false),
        style: {
          color: '#00ff00',
          pixelSize: 8,
          outlineColor: '#ffffff',
          outlineWidth: 2,
        },
      })
      graphicJD.bindTooltip('与地形地面的交点')
      graphicLayer.addGraphic(graphicJD)

      //摄像头朝向的地面点连线
      let graphicLine = new mars3d.graphic.PolylineEntity({
        positions: new Cesium.CallbackProperty(function (time) {
          if (!positionSXT || !positionDM || positionJD != null) {
            return []
          }
          return [positionSXT, positionDM]
        }, false),
        style: {
          width: 2,
          arcType: Cesium.ArcType.NONE,
          color: '#ffff00',
        },
      })
      graphicLayer.addGraphic(graphicLine)

      $('#btnSePoint').click(function (e) {
        map.graphicLayer.clear()
        map.graphicLayer.startDraw({
          type: 'point',
          style: {
            pixelSize: 8,
            color: '#ffff00',
            label: {
              text: '摄像头',
              font_size: 20,
              color: '#ffffff',
              outline: true,
              outlineColor: '#000000',
              pixelOffsetY: -20,
            },
          },
          success: function (graphic) {
            positionSXT = graphic.positionShow
            positionSXT = mars3d.PointUtil.addPositionsHeight(positionSXT, 5.0) //增加杆子高度

            updateModel()
          },
        })
      })

      $('#btnGetCenter').click(function () {
        if (!positionSXT || !positionDM) {
          return []
        }

        sightline.clear()
        sightline.addAsync(positionSXT, positionDM)
      })
    }

    function updateModel() {
      if (!positionSXT) {
        return
      }

      var heading = Cesium.Math.toRadians(Number($('#model_heading').val()))
      var pitch = Cesium.Math.toRadians(Number($('#model_pitch').val()))
      var roll = Cesium.Math.toRadians(Number($('#model_roll').val()))

      var hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll)
      positionDM = mars3d.PointUtil.getRayEarthPosition(positionSXT, hpr, true, map.scene.globe.ellipsoid)

      if (!positionDM) {
        //与地面无交点时
        positionDM = mars3d.PointUtil.getPositionByHprAndLen(positionSXT, hpr, 5000)
      }

      sightline.clear()

      positionJD = null
    }
  </script>
</body>

</html>
